/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package solitaireofdoom.solitaire;

import java.awt.datatransfer.*;
import java.io.*;

/**
 * Creates deck to be used in card drag.
 * 
 */
public class DeckTransferable implements Transferable, ClipboardOwner
{
    public static DataFlavor cardFlavor = null;
    public static DataFlavor localCardFlavor = null;

    private Deck deck;

    static
    {
        try
        {
            cardFlavor = new DataFlavor(Deck.class, "Non local card");
            localCardFlavor = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + "; class=solitaireofdoom.solitaire.Deck", "Local card");
        }
        catch (Exception e)
        {
            System.err.println(e);
        }
    }

    /**
     * Creates deck to be used
     *
     * @param deck <b>Deck</b>
     */
    public DeckTransferable(Deck deck)
    {
        this.deck = deck;
    }

    /**
     * Checks to see if the data types are the same.
     *
     * @param flavor <b>DataFlavor</b>
     * @return <b>Boolean</b>
     */
    public boolean isDataFlavorSupported(DataFlavor flavor)
    {
        return flavor.equals(cardFlavor) || flavor.equals(localCardFlavor);
    }

    /**
     * Fills in array <i>DataFlavors</i> with the data flavor of the card and the location the card is being moved to.
     * @return <i>dataFlavors</i>
     */
    public synchronized DataFlavor[] getTransferDataFlavors()
    {
        DataFlavor[] dataFlavors = new DataFlavor[2];
        dataFlavors[0] = this.cardFlavor;
        dataFlavors[1] = this.localCardFlavor;

        return dataFlavors;
    }

    /**
     * checks for supported data flavor.
     *
     * @param flavor <b>DataFlavor</b>
     * @return <i>deck</i>
     * @throws java.awt.datatransfer.UnsupportedFlavorException
     * @throws java.io.IOException
     */
    public Object getTransferData(DataFlavor flavor)
            throws UnsupportedFlavorException, IOException
    {
        if (flavor.equals(cardFlavor) || flavor.equals(localCardFlavor))
        {
            return deck;
        }
        else
            throw new UnsupportedFlavorException(flavor);
    }

    /**
     * Error called when card is lost
     *
     * @param clipboard <b>Clipboard</b>
     * @param contents <b>Transferable</b>
     */
    public void lostOwnership(Clipboard clipboard, Transferable contents)
    {
        System.out.println("CardTransferable lost ownership of " +
                clipboard.getName());
        System.out.println("data: " + contents);
    }
}
